package com.chengxiaoxiao.ai_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chengxiaoxiao.ai_demo.api.KnowledgeBaseControllerApi;
import com.chengxiaoxiao.ai_demo.entity.KnowledgeBase;
import com.chengxiaoxiao.ai_demo.service.DocumentEmbeddingService;
import com.chengxiaoxiao.ai_demo.service.KnowledgeBaseService;
import com.chengxiaoxiao.ai_demo.vo.PageResult;
import com.chengxiaoxiao.ai_demo.vo.Result;
import com.chengxiaoxiao.ai_demo.vo.form.KnowledgeBaseForm;
import com.chengxiaoxiao.ai_demo.vo.query.KnowledgeBaseQuery;
import com.chengxiaoxiao.ai_demo.vo.vo.KnowledgeSearchVo;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 知识库 接口控制器
 *
 * @Description:
 * @Author: Cheng Xiaoxiao
 * @Date: 2025-03-14 16:50:21
 */
@RestController
@RequestMapping("/knowledge-base")
public class KnowledgeBaseController implements KnowledgeBaseControllerApi {
    @Resource
    private KnowledgeBaseService knowledgeBaseService;
    @Resource
    private DocumentEmbeddingService documentEmbeddingService;


    /**
     * 获取条件查询信息
     *
     * @param knowledgeBaseQuery 查询条件
     * @return 查询封装
     */
    private QueryWrapper<KnowledgeBase> getKnowledgeBaseQueryWrapper(KnowledgeBaseQuery knowledgeBaseQuery) {
        QueryWrapper<KnowledgeBase> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(KnowledgeBase::getCreateTime);
        wrapper.lambda().like(StringUtils.isNotBlank(knowledgeBaseQuery.getBaseName()), KnowledgeBase::getBaseName, knowledgeBaseQuery.getBaseName());
        wrapper.lambda().like(StringUtils.isNotBlank(knowledgeBaseQuery.getBaseDescription()), KnowledgeBase::getBaseDescription, knowledgeBaseQuery.getBaseDescription());
        wrapper.lambda().like(StringUtils.isNotBlank(knowledgeBaseQuery.getEmbeddingModel()), KnowledgeBase::getEmbeddingModel, knowledgeBaseQuery.getEmbeddingModel());
        return wrapper;
    }

    /**
     * 按照条件查询知识库
     *
     * @param knowledgeBaseQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<KnowledgeBase>> list(KnowledgeBaseQuery knowledgeBaseQuery) {
        return Result.success(knowledgeBaseService.list(getKnowledgeBaseQueryWrapper(knowledgeBaseQuery)));
    }

    /**
     * 分页及条件查询知识库
     *
     * @param page               页码
     * @param size               分页大小
     * @param knowledgeBaseQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<KnowledgeBase>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, KnowledgeBaseQuery knowledgeBaseQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<KnowledgeBase> pageParam = new Page<>(page, size);

        knowledgeBaseService.page(pageParam, getKnowledgeBaseQueryWrapper(knowledgeBaseQuery));
        PageResult<KnowledgeBase> pageResult = new PageResult<>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 根据ID获取知识库详情
     *
     * @param id 知识库ID
     * @return 知识库详细信息
     */
    @Override
    @GetMapping("/{id}")
    public Result<KnowledgeBase> detail(@PathVariable("id") String id) {
        return Result.success(knowledgeBaseService.detailById(id));
    }

    /**
     * 增加知识库
     *
     * @param knowledgeBaseForm 知识库增加表单
     * @return 增加后的知识库
     */
    @Override
    @PostMapping
    public Result<KnowledgeBase> add(@ModelAttribute KnowledgeBaseForm knowledgeBaseForm) {
        return Result.success(knowledgeBaseService.saveKnowledgeBase(knowledgeBaseForm));
    }

    /**
     * 修改知识库
     *
     * @param id            知识库ID
     * @param knowledgeBase 知识库修改实体
     * @return 修改后的知识库
     */
    @Override
    @PutMapping("/{id}")
    public Result<KnowledgeBase> updateById(@PathVariable("id") String id, @RequestBody KnowledgeBase knowledgeBase) {
        knowledgeBase.setId(id);
        return Result.success(knowledgeBaseService.updateKnowledgeBaseById(knowledgeBase));
    }


    /**
     * 删除知识库
     *
     * @param id 知识库ID
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        knowledgeBaseService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 删除知识库
     *
     * @param idList 知识库ID数组
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        knowledgeBaseService.removeByIds(idList);
        return Result.success(true);
    }

    /**
     * 根据知识库ID和关键词进行关键词搜索
     *
     * @param id  知识库ID
     * @param key 关键字
     * @return 内容
     */
    @GetMapping("/search/{id}")
    @Override
    public Result<List<KnowledgeSearchVo>> search(@PathVariable("id") String id, @RequestParam("key") String key) {
        return Result.success(documentEmbeddingService.search(id, key));
    }
}
